package com.imufe.chemicalenterprisedangerwarning.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.imufe.chemicalenterprisedangerwarning.entity.po.ChemicalPO;
import com.imufe.chemicalenterprisedangerwarning.entity.query.ChemicalQuery;
import com.imufe.chemicalenterprisedangerwarning.entity.vo.ChemicalDetailedVO;
import com.imufe.chemicalenterprisedangerwarning.entity.vo.ChemicalVO;
import com.imufe.chemicalenterprisedangerwarning.mapper.ChemicalMapper;
import com.imufe.chemicalenterprisedangerwarning.service.ChemicalService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.imufe.chemicalenterprisedangerwarning.util.BeanUtils;
import com.imufe.chemicalenterprisedangerwarning.util.ListUtils;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 化学品 服务实现类
 * </p>
 *
 * @author BaiBan
 * @since 2022-09-21
 */
@Log4j2
@Service
public class ChemicalServiceImpl extends ServiceImpl<ChemicalMapper, ChemicalPO> implements ChemicalService {

    @Autowired
    ChemicalMapper mapper;

    @Override
    public IPage<ChemicalDetailedVO> page(Long current) {
        Page<ChemicalDetailedVO> page = new Page<>(current, 10);
        mapper.page(page, new QueryWrapper<>());
        return page;
    }

    @Override
    public List<ChemicalVO> optionList() {
            List<ChemicalPO> poList = mapper.selectList(null);
            // po 转 vo
            return ListUtils.copyList(poList, ChemicalVO.class);
    }

    @Override
    public ChemicalDetailedVO detailed(Long id) {
        ChemicalPO po = mapper.selectById(id);
        return BeanUtils.copyPOJO(po, ChemicalDetailedVO.class);
    }

    @Override
    public ChemicalDetailedVO create(ChemicalQuery query) {
        ChemicalPO po = BeanUtils.copyPOJO(query, ChemicalPO.class);
        log.info(po);
        mapper.insert(po);
        return this.detailed(po.getId());
    }

    @Override
    public ChemicalDetailedVO update(ChemicalQuery query) {
        ChemicalPO po = BeanUtils.copyPOJO(query, ChemicalPO.class);
        log.info(po);
        mapper.updateById(po);
        return this.detailed(po.getId());
    }
}
